package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import cjgl.entity.TbClass;
import cjgl.entity.Teacher;
import service.TeacherService;

/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/tea")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private TeacherService teacherService=new TeacherService();  
    private ObjectMapper mapper=new ObjectMapper();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method =request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("update".contentEquals(method)) {
			update(request,response);
		}else if("save".contentEquals(method)) {
			sava(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request, response);
		}else if("add".contentEquals(method)) {
			add(request, response);
		
		}else if("listTea".contentEquals(method)) {	
			request.getRequestDispatcher("teacher/listTea.jsp").forward(request, response);
		}else if("allTea".contentEquals(method)) {
			allTea(request, response);

		}else if("ajaxEdit".contentEquals(method)) {
			ajaxEdit(request, response);

	}
		}

	private void ajaxEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id = Integer.valueOf(request.getParameter("id"));
		Teacher model=teacherService.findById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/teacher/ajaxEdit.jsp").forward(request, response);
	    System.out.println(model);
	}
		


	private void allTea(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		List<Teacher> list = teacherService.findAll();
		String str =mapper.writeValueAsString(list);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(str);
		out.close();
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("jsp/teacher/edit.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr =request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		teacherService.delete(id);
		response.sendRedirect("tea");
	}

	private void sava(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr =request.getParameter("id");
		Integer id=null;
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String username= request.getParameter("username");
		Teacher teacher=new Teacher(id,name,password,username);
		teacherService.sava(teacher);
		response.sendRedirect("tea");
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr =request.getParameter("id");
		Integer id=Integer.parseInt(idStr);
		Teacher model = teacherService.findById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/teacher/edit.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Teacher> list = teacherService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/teacher/list.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
	